/**
 * Created by Administrator on 2017/6/29 0029.
 */
"use strict";
angular.module('app.sensor').controller('DataCtrl', function ($scope,$http,$interval) {
    // 获取该端口的字段名称
      $scope.fields = {};
      // $http.get('/list_nopage/device_parameters?type==4&pro_type==8053').then(function(res){
      //   // console.log(res.data);
      //   if (res.data.ret == 1) {
      //     var arr = res.data.info;   
      //     for (var i = 0; i < arr.length; i++) {
      //       var obj = arr[i];
      //       $scope.fields[obj.key1] = obj.value1;  
      //     }
      //      // console.log($scope.fields);
      //   }
      // }) 
      $http.get(baseurl+'/table_columns/log_8053').then(function(res){
            if (res.data.ret == 1) {

                for(var i = 0 ; i < res.data.info.length; i++){
                    var obj = res.data.info[i];                    
                    if(obj.COLUMN_NAME === 'id'){
                        obj.column_comment = '编号';
                    }else if(obj.COLUMN_NAME === 'deviceid'){
                        obj.column_comment = '设备编号';
                    }else if(obj.COLUMN_NAME === 'fid'){
                        obj.column_comment = '公司';
                    }else if(obj.COLUMN_NAME === 'projectid'){
                        obj.column_comment = '项目';
                    }
                    else if (obj.COLUMN_NAME === 'groupid') {
                        obj.column_comment = '组';
                    }
                    $scope.fields[obj.COLUMN_NAME] = obj.column_comment;
                }
                sends();
            }
      });
    // 实时数据
    //配置分页基本参数
    var sends = function () {
       $http.get(baseurl+'/actual_data/8053').then(function(res){
           // console.log(res.data.info);
           // alert(JSON.stringify(res.data.info[0]));
           var DATAS = [];
           if(res.data.info.length > 0){
                for (var key in res.data.info[0]) {
                    // console.log(key)
                    if(key !== 'deviceid' && key !== 'dev_type' && key !== 'dev_type_id'){
                        var obj = {};
                        if ($scope.fields[key]) {
                            obj.name = $scope.fields[key];
                        }else{
                          obj.name = key;
                        }
                        obj.key = key;
                        obj.data = parseFloat(res.data.info[0][key]);
                        obj.data = obj.data.toFixed(2);
                        DATAS.push(obj);
                    }
                }
               $scope.devs = res.data.info;
               $scope.DATAS = DATAS;
           }
          if (res.data.ret == 1) {
            $scope.realdata = res.data.info;
            $('.blink').css('color','black');  //默认值
            setTimeout(" $('.blink').css('color','blue')",200); //第一次闪烁
            setTimeout( "$('.blink').css('color','black')",1000); //第二次闪烁
          }
        })
    }
    
     var timeout = $interval(sends, 10000);
     $scope.$on('$destroy', function () {
         $interval.cancel(timeout);
     })
      //$scope.min = {};
      //$scope.sensor_data = function(){
      //   if ($scope.deviceid == '') {
      //     alert("请输入模拟设备id");
      //     return;
      //   }
      //   var data = {
      //     data:$scope.min,
      //     data_ext:{
      //        // press_unit:'pa',
      //        deviceid:$scope.min.deviceid

      //     }
      //   }
      //   $http.post(baseurl+'/sensor_data_public2/8053',data).then(function(res){
      //      // console.log(res.data);
      //      if (res.data.ret == 1) {
      //         new Notify('success','发送成功');
      //      }
      //   })
      //}
      // 测试
      // $http.get(baseurl+'/history_data_yk?deviceid==00010001&time=2017-12-28,2018-01-02').then(function(res){
          // console.log(res.data);
      // });




// // -----------------------------------------------------------------------
        $http.get(baseurl+'/actual_data/8053').then(function(res){
           console.log(res.data);
           // $scope.devs2 = res.data.info;
          // $scope.devp = res.data.info;
             var height=$("#shu").innerHeight()
             var type_id=[],value1=[],value2=[],table_deviceid=[],table_dev_type=[],table_time=[],table_status=[],table_deviceerror=[],table_groupid=[]
             $.each(res.data.info,function(i,v){
              type_id.push(v.dev_type_id)
              value1.push(v.value1)
              value2.push(v.value2)
              table_deviceid.push(v.deviceid)
              table_dev_type.push(v.dev_type)
              table_time.push(v.time)
              table_status.push(v.devicestatus)
              table_deviceerror.push(v.deviceerror)
              table_groupid.push(v.groupid)
             })
             // console.log(type_id)
             $(".minblock").html("")     
             
             for(var i=0;i<type_id.length;i++){
                
                if(type_id[i]==1){                 
                  var html="";
                      html+='<div class="smclock col-xs-12 col-sm-6  col-lg-3">'
                          +   '<div class="shuBox">'
                          +       '<div class="shu">'
                          +           '<span class="c">总量5L</span>'
                          +           '<div class="a">'
                          +              '<span class="aspan"></span>'
                          +           '</div>'
                          +           '<img  class="table_none" alt="">'
                          +       '</div>'
                          +   '</div>'
                          + '<div class="right_box">'
                          +   '<div style="display:block;"">'
                          +     '<span class="span2 span3" >设备号 : </span>'
                          +     '<span class="table_b span2 span3"></span>'
                          +   '</div>'
                          +   '<div>'
                          +     '<span class="span2">类型 : </span>'
                          +     '<span class="table_type"></span>'
                          +   '</div>'
                          +   '<div>'
                          +     '<span class="span2">设备状态 : </span>'
                          +     '<span class="table_status"></span>'
                          +   '</div>'
                          +   '<div>'
                          +     '<span class="span2">设备故障 : </span>'
                          +     '<span class="table_error"></span>'
                          +   '</div>'
                          +   '<div>'
                          +     '<span class="span2">组 : </span>'
                          +     '<span class="table_groupid"></span>'
                          +   '</div>'
                          + '</div>'
                          +'</div>'
                  $(".minblock").append(html)
                  // console.log(table_deviceid[i])
                  $(".table_b").eq(i).text(table_deviceid[i])
                  $(".table_type").eq(i).text(table_dev_type[i])
                  $(".table_time").eq(i).text(table_time[i])
                  $(".table_status").eq(i).text(table_status[i])
                  $(".table_error").eq(i).text(table_deviceerror[i])
                  $(".table_groupid").eq(i).text(table_groupid[i])

                  var height=$(".shu").innerHeight()
                  if(value2[i]==0 || value1[i]==0){
                    $(".aspan").text(0+"%"+"("+value2[i]+"L"+")").css("top",-35+"px")
                  }else{
                    $(".aspan").text((value1[i]/value2[i])+"%"+"("+value2[i]+"L"+")")
                  }

                }else if(type_id[i]==2){
                  var html="";
                      html+='<div class="smclock col-xs-12 col-sm-6  col-lg-3">'
                          +   '<div class="hengBox">'
                          +       '<div class="heng">'
                          +           '<span class="d">总量5L</span>'
                          +           '<div class="b">'
                          +              '<span class="bspan"></span>'
                          +           '</div>'
                          +           '<img  class="table_none" alt="">'
                          +       '</div>'
                          +   '</div>'
                          + '<div class="right_box">'
                          +   '<div style="display:block;"">'
                          +     '<span class="span2 span3" >设备号 : </span>'
                          +     '<span class="table_b span2 span3"></span>'
                          +   '</div>'
                          +   '<div>'
                          +     '<span class="span2">类型 : </span>'
                          +     '<span class="table_type"></span>'
                          +   '</div>'
                          +   '<div>'
                          +     '<span class="span2">设备状态 : </span>'
                          +     '<span class="table_status"></span>'
                          +   '</div>'
                          +   '<div>'
                          +     '<span class="span2">设备故障 : </span>'
                          +     '<span class="table_error"></span>'
                          +   '</div>'
                          +   '<div>'
                          +     '<span class="span2">组 : </span>'
                          +     '<span class="table_groupid"></span>'
                          +   '</div>'
                          +  '</div>'
                          + '</div>'
                  $(".minblock").append(html)
                  $(".table_b").eq(i).text(table_deviceid[i])
                  $(".table_type").eq(i).text(table_dev_type[i])
                  $(".table_time").eq(i).eq(i).eq(i).text(table_time[i])
                  $(".table_status").eq(i).eq(i).text(table_status[i])
                  $(".table_error").eq(i).eq(i).text(table_deviceerror[i])
                  $(".table_groupid").eq(i).text(table_groupid[i])
                  var width=$(".heng").innerWidth()
                  if(value2[i]==0 || value1[i]==0){
                    $(".bspan").text(0+"%"+"("+value2[i]+"L"+")").css("left",50%+"px")

                  }else{
                    $(".bspan").text((value1[i]/value2[i]) +"%"+" ("+value2[i]+"L"+")")
                  }
                }else{
                  var html="";
                      html+='<div class="smclock col-xs-12 col-sm-6  col-lg-3">'
                          +   '<div class="instrument">'
                          +     '<div>'
                          +        '<div>' 
                          +           '<span class="numa">70%</span>'
                          +           '<div class="numb">总油量5L'
                          +              '<span class="table_none"></span>'
                          +           '</div>'
                          +           '<img class="pointer" src="../../styles/img/Pointer.png" alt="">'
                          +        '</div>'
                          +     '</div>'
                          +   '</div>'
                          + '<div class="right_box">'
                          +   '<div style="display:block;"">'
                          +     '<span class="span2 span3" >设备号 : </span>'
                          +     '<span class="table_b span2 span3"></span>'
                          +   '</div>'
                          +   '<div>'
                          +     '<span class="span2">类型 : </span>'
                          +     '<span class="table_type"></span>'
                          +   '</div>'
                          +   '<div>'
                          +     '<span class="span2">设备状态 : </span>'
                          +     '<span class="table_status"></span>'
                          +   '</div>'
                          +   '<div>'
                          +     '<span class="span2">设备故障 : </span>'
                          +     '<span class="table_error"></span>'
                          +   '</div>'
                          +   '<div>'
                          +     '<span class="span2">组 : </span>'
                          +     '<span class="table_groupid"></span>'
                          +   '</div>'
                          +  '</div>'
                          + '</div>'
                  $(".minblock").append(html)
                  $(".table_b").eq(i).text(table_deviceid[i])
                  $(".table_type").eq(i).text(table_dev_type[i])
                  $(".table_time").eq(i).text(table_time[i])
                  $(".table_status").eq(i).text(table_status[i])
                  $(".table_error").eq(i).text(table_deviceerror[i])
                  $(".table_groupid").eq(i).text(table_groupid[i])
                  var rotate,percentage;
                  if(value2[i]==0 || value1[i]==0){
                    percentage=0
                  }else{
                    percentage=value1[i]/value2[i]
                  }

                  if(percentage<=0){
                    rotate=-90
                  }else if(percentage>0 && percentage<0.5){
                    rotate=-percentage*90
                  }else if(percentage==0.5){
                    rotate=90
                  }else if(percentage>0.5 && percentage<1){
                    rotate=percentage*90
                  }else if(percentage>=1){
                    rotate=90
                  }

                  if(percentage==0){
                    $(".numa").text(0+"%"+"("+value2[i]+"L"+")")
                    $(".pointer").css("transform","rotate("+rotate+"deg)")
                  }
             }            
          }

      
        })




});
